package com.sz.common.pay.citic.xml;

import com.sz.common.pay.citic.entity.query.DetailInfoRes;
import com.sz.common.pay.citic.entity.query.SummaryInfoRes;
import com.sz.common.pay.citic.entity.transfer.ExternalReq;
import com.sz.common.pay.citic.entity.transfer.ExternalRes;
import com.sz.common.pay.citic.entity.transfer.InternalReq;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by siqishangshu on 17/10/16.
 * this is for the Special filed when bean ,xml convert
 *
 * the map key(bean name)->List String value(every value beanString -#- xmlString)
 * when bean to xml use xmlString XmlFormatUtil beanString
 * when xml to bean use beanString XmlFormatUtil xmlString
 * it will static initialized
 */
public class XmlFormatUtil {

    public static final Map<Class,List<String>> OBJFIELD ;
    public static final String SPLITOR = "#" ;

    static{
        OBJFIELD = new HashMap<>();

        //InternalReq
        List<String> internalReqList = new ArrayList<>();
        internalReqList.add("abstract_"+SPLITOR+"abstract");
        OBJFIELD.put(InternalReq.class,internalReqList);
        //InternalRes
        List<String> InternalResList = new ArrayList<>();
        InternalResList.add("abstract_"+SPLITOR+"abstract");
        OBJFIELD.put(InternalReq.class,InternalResList);
        //ExternalReq
        List<String> externalReqList = new ArrayList<>();
        externalReqList.add("abstract_"+SPLITOR+"abstract");
        OBJFIELD.put(ExternalReq.class,externalReqList);
        //ExternalRes
        List<String> externalResList = new ArrayList<>();
        externalResList.add("abstract_"+SPLITOR+"abstract");
        OBJFIELD.put(ExternalRes.class,externalResList);
        //DetailInfoRes
        List<String> detailInfoResList = new ArrayList<>();
        detailInfoResList.add("abstract_"+SPLITOR+"abstract");
        OBJFIELD.put(DetailInfoRes.class,detailInfoResList);
        //DetailInfoRes
        List<String> summaryInfoResList = new ArrayList<>();
        summaryInfoResList.add("abstract_"+SPLITOR+"abstract");
        OBJFIELD.put(SummaryInfoRes.class,summaryInfoResList);

        //DetailedInfoRes
        List<String> detailedInfoResList = new ArrayList<>();
        detailedInfoResList.add("abstract_"+SPLITOR+"abstract");
        OBJFIELD.put(DetailInfoRes.class,detailedInfoResList);

    }


    /**
     * format the titic xml
     *
     citic doesn't need this filed
        <standalone="yes">
     the special filed
        <list name="userDataList">
            <row>
            </row>
        </list>
     * @param xml
     * @return
     */
    public static String toXml(String xml,Class clazz){
        String newXml = xml.replace(" standalone=\"yes\"","")
                .replace("<list>", "<list name=\"userDataList\">");
        if (!OBJFIELD.containsKey(clazz)){
            return newXml;
        }else{
            for (String string : OBJFIELD.get(clazz)){
                newXml = newXml.replace(string.split(SPLITOR)[0],string.split(SPLITOR)[1]);
            }
            return newXml;
        }
    }

    /**
     * format the jaxb xml
     *
     jaxb doesn't need this filed
        <standalone="yes">
     the special filed
        <list name="userDataList">
            <row>
            </row>
        </list>
     * @param xml
     * @return
     */
    public static String toObj(String xml,Class clazz){
        String newXml = xml.replace("name=\"userDataList\"" , "");
        if (!OBJFIELD.containsKey(clazz)){
            return newXml;
        }else{
            for (String string : OBJFIELD.get(clazz)){
                newXml = newXml.replace(string.split(SPLITOR)[1],string.split(SPLITOR)[0]);
            }
            return newXml;
        }
    }
}
